(function($, window, document, undefined) {
	//定义分页类
	function Paging(element, options) {
		this.element = element;
		//传入形参
		this.options = {
			pageNo: options.pageNo||1,
			numPerPage: options.numPerPage,
			totalSize:options.totalSize,
			maxPageNo : options.maxPageNo,
			callback:options.callback
		};
		//根据形参初始化分页html和css代码
		this.init();
	}
	//对Paging的实例对象添加公共的属性和方法
	Paging.prototype = {
		constructor: Paging,
		init: function() {
			this.creatHtml();
			this.bindEvent();
		},
		creatHtml: function() {
			var me = this;
			var content = "";
			var current = me.options.pageNo;
			// var total = me.options.numPerPage;
			var totalNum = me.options.totalSize;
			content += "<a id=\"firstPage\"><<</a><a id='prePage'><</a>";//
			//根据查询到的信息的总数量和每一页数据的数量之比计算总的页数。
			me.options.totalPage=Math.ceil(me.options.totalSize/me.options.numPerPage);
			total=me.options.totalPage;
			var maxPageNo = 6;//me.options.maxPageNo;
			if(total <= maxPageNo){
				for(var i = 1; i < total + 1; i++) {
					if(current == i) {
						content += "<a class='current'>" + i + "</a>";
					} else {
						content += "<a>" + i + "</a>";
					}
				}
			}else{
				if(current <= (total - maxPageNo + 1)){
					for(var i = current; i <= (current + maxPageNo - 1); i++) {
						if(current == i) {
							content += "<a class='current'>" + i + "</a>";
						} else {
							content += "<a>" + i + "</a>";
						}
					}
				}else if(current > (total - maxPageNo + 1)){
					for(var i = (total - maxPageNo + 1); i <= total; i++) {
						if(current == i) {
							content += "<a class='current'>" + i + "</a>";
						} else {
							content += "<a>" + i + "</a>";
						}
					}
				}
			}
			content += "<a id='nextPage'>></a>";
			content += "<a id=\"lastPage\">>></a>";
			content += "<span class='totalPages'> 共<span>"+total+"</span>页 </span>";
			content += "<span class='totalSize'> 共<span>"+totalNum+"</span>条记录 </span>";
			me.element.html(content);
			// var num = parseInt((parseInt($("#page").css("width")) - 10*(maxPageNo + 4))/(maxPageNo + 4));
			$(".page_div").find("a").css("width", 30 + "px");
			$(".page_div").find("a").css("width", 30 + "px");
		},
		//添加页面操作事件
		bindEvent: function() {
			var me = this;
			me.element.off('click', 'a');
			me.element.on('click', 'a', function() {
				var num = $(this).html();
				var id=$(this).attr("id");
				if(id == "prePage") {
					if(me.options.pageNo == 1) {
						me.options.pageNo = 1;
					} else {
						me.options.pageNo = +me.options.pageNo - 1;
					}
				} else if(id == "nextPage") {
					if(me.options.pageNo == me.options.totalPage) {
						me.options.pageNo = me.options.totalPage
					} else {
						me.options.pageNo = +me.options.pageNo + 1;
					}

				} else if(id =="firstPage") {
					me.options.pageNo = 1;
				} else if(id =="lastPage") {
					me.options.pageNo = me.options.totalPage;
				}else{
					me.options.pageNo = +num;
				}
				me.creatHtml();
				if(me.options.callback) {
					me.options.callback(me.options.pageNo);
				}
			});
		}
	};
	//通过jQuery对象初始化分页对象
	$.fn.paging = function(options) {
		return new Paging($(this), options);
	}
})(jQuery, window, document);